package com.heatup.apt.management.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.heatup.apt.common.dao.ActivityInfoMapper;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.management.service.coupon.ActivityService;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.coupon.ActivityInfo;

/**
 * 活动控制器
 * @author jixiangyun
 * 2016年12月1日 下午3:57:18
 */
@Controller
@RequestMapping("system/activity")
public class ActivityController extends BaseController {
	
	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private ActivityInfoMapper activityInfoMapper;
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/to_activity_list")
	public ModelAndView to_activity_list(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		Map<String,Object> map = new HashMap<String,Object>();
		String activityName = RequestTools.RequestString(request, "activityName", null);
		String inviteCouponId = RequestTools.RequestString(request, "inviteCouponId", null);
		String couponId = RequestTools.RequestString(request, "couponId", null);
		int pageNum = RequestTools.RequestInt(request, "pageNum",1);
		map.put("activityName", activityName);
		map.put("inviteCouponId", inviteCouponId);
		map.put("couponId", couponId);
		map.put("pageNum", pageNum);
		model.addAllObjects(activityService.queryActivityInfoList(map));
		model.setViewName("operationsmanagement/activity_list");
		return model;
	}
	
	
	@RequestMapping("/to_activity_add")
	public ModelAndView to_activity_add(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		model.setViewName("operationsmanagement/activity_add");
		return model;
	}
	
	@ResponseBody
	@RequestMapping("/activity_save")
	public String to_activity_save(ActivityInfo activityInfo,HttpServletRequest request){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		ModelAndView model = new ModelAndView();
		if(activityInfo.getId() == null){
			activityInfo.setCreateBy(this.getUserInfo().getId()+"");
		}
		activityInfo.setStatus("1");
		int bool = activityService.saveActivity(activityInfo);
		model.setViewName("operationsmanagement/activity_add");
		/*return new ModelAndView("redirect:to_activity_list");*/
		
		apiResultModel.setData(bool);
		
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	@RequestMapping("/to_activity_edit")
	public ModelAndView to_activity_edit(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		int id = RequestTools.RequestInt(request, "id",0);
		/*ActivityInfo activityInfo = activityInfoMapper.selectByPrimaryKey(id);*/
		Map<String,Object> map = activityService.activityEdit(id);/*new HashMap<String,Object>();*/
		/*map.put("activityInfo", activityInfo);*/
		model.addAllObjects(map);
		model.setViewName("operationsmanagement/activity_edit");
		return model;
	}
	
	
}
